import Vue from 'vue'
import App from './App.vue'
import vueRouter from 'vue-router'
import Login from './login.vue'
import Home from './home.vue'
import User from './component/User.vue'
import Role from './component/Role.vue'
import axios from 'axios';
import ElementUI from 'element-ui';
import {Message} from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import VueRouter from 'vue-router';
import store from './store'
Vue.use(ElementUI);
Vue.use(vueRouter);
axios.defaults.withCredentials=true
Vue.prototype.$ajax=axios;
const routes = [
  {
  path:'/',component:Login
  },
  {
    path:'/home',component:Home,
    children:[
      {
        path:'/',name:'',component:Home,children:[
          {
            path:'/user',component:User,name:''
          },
          {
            path:'/role',component:Role,name:''
          }
        ]
      }
    ]
  }
]

const router = new VueRouter({routes})

axios.interceptors.response.use(response => {
// Do something before response is sent
return response;
},error => {
  console.log('axios.interceptors.response{}',error.response);
  if(error.response.status==401){
    Message.error("尚未登录");
    localStorage.removeItem('user');
    store.state.rs=[];
    store.state.user='';
    router.replace({path:"/"});
    return;
  }
  if(error.response.status==428){
    Message.error(error.response.data.msg);
    return;
  }
// Do something with response error
return Promise.reject(error);
});


router.beforeEach((to, from, next) => {
  if(to.path=='/') {
    next();
    return
  }
  var username = store.state.user.username;
  console.log(username);
  if(username=='尚未登录' || username ==undefined ){
    next({path:'/'});
    return;
  }
    console.log("hhhhhhhh{}",store.state.rs);
    if(store.state.rs.length == 0){
      console.log("000000000000000000000000000000000{}",store.state.rs);
      initMenu(router,store);
      setTimeout(() => {
        next();
      }, 1000);
      return;
    }
     next();
})

export  const initMenu=(router,store)=>{
  axios({
      url:"http://localhost:8081/menu/getMenuByUserID",
      method:"get"
  }).then(function (param) { 
      console.log("<><><><>{}",param);
    
      if(param){
          var routes =  formRouters(param.data.object);
          console.log("routests为:{}",routes);
          //把vue路由的数组赋值给 vue router对象
          router.options.routes[1].children = routes;
          console.log("-----{}",router.options.routes[1].children);
          store.commit('initMenu',routes);
      }        
   })
}

export const formRouters = (routers)=>{

  // console.log('=============================>{}',routers);
  let array = [];
  //json 数据转成vue 识别的路由格式
  routers.forEach(element => {
          let children = [];
          if(element.menuList && element.menuList instanceof Array){
              children =  formRouters(element.menuList);
          }
          let r = {
              path:element.path,
              component(resolve){
                  if(element.component.startsWith('User')){
                      require(['./component/' + element.component + '.vue'], resolve)
                  }else if(element.component.startsWith('Role')){
                      require(['./component/' + element.component + '.vue'], resolve)  
                  }
              },
              name:element.name,
              children:children
          }
          array.push(r);   
  });
  return array;
}


new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
